package com.ccx.coachsite.ui.own;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.Account;
import com.ccx.coachsite.bus.AccountMgr;
import com.ccx.coachsite.bus.GetVerifyCodeBus;
import com.ccx.coachsite.bus.RepasswordBus;
import com.ccx.coachsite.util.CommonViewUtils;
import com.ccx.coachsite.util.StringUtil;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.Toast;

public class OwerMoreRepasswordView extends OwerBaseView
{
	private View mSave; // 保存

	private EditText mOldPassword; // 旧密码
	private EditText mNewPassword; // 新密码
	private EditText mRetryPwd; // 确认密码
	private EditText mVerityCode; // 验证码
	private View mVerifyBtn; // 获取验证
	
	private RepasswordBus bo;
	
	private Account account;
	
	public OwerMoreRepasswordView(Context context, OwerMainBase superView) 
	{
		super(context, superView);
		account = AccountMgr.readAccountInfo(context);
		mOldPassword = (EditText) findViewById(R.id.old_pwd);
		mNewPassword = (EditText) findViewById(R.id.new_pwd);
		mRetryPwd = (EditText) findViewById(R.id.retry_pwd);
		mVerityCode = (EditText) findViewById(R.id.repassword_verify_code);
		mVerifyBtn = findViewById(R.id.repassword_btn_verify);
		mVerifyBtn.setOnClickListener(new OnGetVerify());
		
		mSave = findViewById(R.id.repassword_btn);
		mSave.setOnClickListener(new OnSave());
	}
	
	public void setEmpty()
	{
		mOldPassword.setText("");
		mNewPassword.setText("");
		mRetryPwd.setText("");
		mVerityCode.setText("");
	}
	
	class OnGetVerify implements OnClickListener {
		@Override
		public void onClick(View v) {
			String mobilenumber = account.getMobile();
			GetVerifyCodeBus bo = new GetVerifyCodeBus(mContext);
			bo.getVerifyCode(mobilenumber);
		}
	}

	/**
	 * 发送变更请求
	 */
	private void sendRequest() {
		if (!checkPwd())
			return;

		if (!CommonViewUtils.isNetConnectionAvailable(mContext)) {
			Toast.makeText(mContext, R.string.login_network_error, Toast.LENGTH_SHORT).show();
			return;
		}

		if (bo == null) {
			bo = new RepasswordBus(mContext, this);
		}

		final String newPwd = mNewPassword.getText().toString();
		final String oldPwd = mOldPassword.getText().toString();
		final String code = mVerityCode.getText().toString();
		bo.changePassword(account, newPwd, oldPwd, code);
	}

	class OnSave implements OnClickListener {
		@Override
		public void onClick(View v) {
			sendRequest();
		}
	}

	/**
	 * 密码是否合法
	 * @return
	 */
	private boolean checkPwd() {
		Animation shake = AnimationUtils.loadAnimation(mContext, R.anim.shake);

		String oldPwdStr = mOldPassword.getText().toString().trim();
		String newPwdStr = mNewPassword.getText().toString().trim();
		String retryPwdStr = mRetryPwd.getText().toString().trim();
		String verifyCode = mVerityCode.getText().toString().trim();
		
		
		if (StringUtil.isEmpty(oldPwdStr)) {
			CommonViewUtils.showErrorByEditText(mOldPassword, mContext.getResources().getString(R.string.change_pwd_empty), shake);
			return false;
		}
		
		/*
		if (!oldPwdStr.equals(account.getPassword())) {
			CommonViewUtils
					.showErrorByEditText(mOldPassword, mContext.getResources().getString(R.string.change_pwd_old_pwd_wrong), shake);
			return false;
		}
		*/
		if (StringUtil.isEmpty(newPwdStr)) {
			CommonViewUtils
					.showErrorByEditText(mNewPassword, mContext.getResources().getString(R.string.change_pwd_new_pwd_empty), shake);
			return false;
		}

		if (!StringUtil.isPwdValid(newPwdStr)) {
			CommonViewUtils.showErrorByEditText(mNewPassword, mContext.getResources().getString(R.string.change_pwd_valid), shake);
			return false;
		}

		if (StringUtil.isEmpty(retryPwdStr)) {
			CommonViewUtils.showErrorByEditText(mRetryPwd, mContext.getResources().getString(R.string.change_pwd_retry_pwd_empty), shake);
			return false;
		}

		if (!newPwdStr.equals(retryPwdStr)) {
			CommonViewUtils
					.showErrorByEditText(mRetryPwd, mContext.getResources().getString(R.string.change_pwd_retry_pwd_nosame), shake);
			return false;
		}
		
		if (StringUtil.isEmpty(verifyCode)) {
			CommonViewUtils.showErrorByEditText(mVerityCode, mContext.getResources().getString(R.string.register_verify_code_empty),
					shake);
			return false;
		}

		return true;
	}
}
